
Ext.define('FrontSuite.view.TableTreePanel', {
    extend:       'Ext.tree.Panel',  // defines the superclass. results in class inheritance.

    id           : 'table-tree-panel',
    scroll       : false,
    bodyStyle    : {border:0},
    useArrows    : true,
    rootVisible  : false,
    store        : null,
    contextMenu  : null,

    constructor: function (id) {
        if (id) {
            this.id = id;
        }
        var testMode = FrontSuite.options.test_mode;
        var httpMethod = testMode ? 'GET' : 'POST';
        // console.log('TableTreePanel: httpMethod='+httpMethod);

        this.store = Ext.create('Ext.data.TreeStore', {
            storeId  : 'table.tree.store',
            autoLoad : true,
            proxy : {
                type : 'ajax',
                url  : FrontSuite.options.apps_urldir + '/sitefront/front-framework/api.php/service/ext/tabletree',
                extraParams: {
                    mode : 'getTree'
                },
                actionMethods : 'GET'
            },
            root: {
                text     : 'Tree',
                id       : 'src',
                expanded : true
            }
        });

        this.contextMenu = new Ext.menu.Menu({
            items: [
                { id: 'table-tree-menu-delete', text: 'Delete' }
            ],
            listeners: {
                click: function(menu, item, e, eopts) {
                    if (FrontSuite.session.lastTableTreeIdClicked) {
                        var tableNodeId, tableParentId, tableLeaf;
                        tableNodeId   = FrontSuite.session.lastTableTreeIdClicked;
                        tableParentId = FrontSuite.session.lastTableParentIdClicked;
                        tableLeaf     = FrontSuite.session.lastTableLeafClicked;
    
                        if (tableLeaf == true) {
                            FrontSuite.util.ext.delete({ type: 'grid', key: tableNodeId }, function (response, message) {
                                if (response.success) {
                                    var selectedNode = Ext.getCmp('table-tree-panel').getSelectionModel().getSelection()[0];
                                    selectedNode.remove();
                                }
                                else {
                                    Ext.MessageBox.show({
                                       title   : 'Status',
                                       msg     : 'server-side failure: ' + message,
                                       buttons : Ext.MessageBox.OK
                                    });
                                }
                            });
                        }
    
                        FrontSuite.session.lastTableTreeIdClicked = null;
                    }
                }
            }
        });

        this.superclass.constructor.call(this, id);
        return(this);
    },

    listeners    : {
        itemclick : function(view, rec, item, index, eventObj) {
            var id      = rec.data.id;
            // console.log('TableTreePanel.listeners.itemclick(rec.data.id='+rec.data.id+',rec.get(id)='+rec.get('id')+')');
            if (FrontSuite.app.isViewableTreeId(id)) {
                FrontSuite.app.addTab(id);
            }
        },
        itemcontextmenu: function(view, rec, node, index, e) {
            e.stopEvent();
            this.contextMenu.showAt(e.getXY());
            FrontSuite.session.lastTableTreeIdClicked   = rec.data.id;
            FrontSuite.session.lastTableParentIdClicked = rec.parentNode.data.id;
            FrontSuite.session.lastTableLeafClicked     = rec.data.leaf;
            return false;
        }
    },

    refresh: function () {
        this.store.reload();
        //FrontSuite.app.message('Status','Refreshing table tree...');
    }
});

